package server;

import java.io.File;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Vector;

import sameClasses.NetworkPacket;
import sameClasses.User;


public class Users implements Serializable {
  private static final long serialVersionUID = 1L;
  static final int myVersion = 1;
  boolean activated;
  Vector<User> userlist;
  File db = new File("data/userDB");

  public Users() {
    this.userlist = new Vector<User>();
  }

  public boolean userOk(User user) {
    if (this.userlist.contains(user) && this.userlist.elementAt(this.userlist.indexOf(user)).isLoggedIn()) {
      System.out.println("[Users] User found in DB");
      return true;
    }
    System.out.println("[Users] ERROR: User NOT found in DB");
    return false;
  }
  
  public boolean changeUserPw(User user, char[] newPassword) throws NoSuchAlgorithmException, InvalidKeySpecException {
    if (this.userlist.contains(user)) {
      this.userlist.get(this.userlist.indexOf(user)).changePassword(newPassword);
      return true;
    }
    return false;
  }

  public boolean add(User newUser) {
    if (this.userlist.contains(newUser)) {
      System.out.println("ADD: nope");
    } else {
      this.userlist.add(newUser);
      System.out.println("Users ADD: yep");
      return true;
    }
    return false;
  }

  public boolean delete(User oldUser) {
    if(this.userlist.contains(oldUser)) {
      System.out.println("DELETE: user deleted");
      this.userlist.remove(oldUser);
      return true;
    } else {
      System.out.println("DELETE: no user found");
      return false;
    }
  }

  public void setActivation(boolean activation) {
    if (this.activated != activation)
      this.activated = activation;
  }

  public boolean getActivation() {
    return this.activated;
  }


  public void test() {
  }
  
  public String getName() {
    return "Usermanagement";
  }

  public int getId() {
    return 1;
  }

  public Object getData(NetworkPacket incoming) {
    return this.userlist;
  }

  public boolean update(Object data, User user) {
    return false;
  }

  public String getDb() {
    return new String("data/userDB");
  }

  public boolean setList(Vector<?> input) {
    Vector<User> test = new Vector<User>();
    if (input.getClass() == test.getClass()) {
      this.userlist = (Vector<User>) input;
      return true;
    }
    return false;
  }

  public Vector<?> getList() {
    if(this.userlist == null) {
      this.userlist = new Vector<User>();
      return this.userlist;
    }
    return this.userlist;
  }

  public Object getData(Object what, User user) {
    if(what instanceof Vector) {
      return this.userlist;
    } 
    return new Vector<User>();
  }

  public void setId(int id) {
  }
  
  public byte[] getSalt(User user) {
    if(this.userlist.contains(user)) {
      return this.userlist.elementAt(this.userlist.indexOf(user)).getSalt();
    }
    return new byte[0];
  }

  public boolean checkName(String name) {
    for(User usr:this.userlist) {
      if(usr.getName().equals(name)) {
        System.out.println("Users-Server: checkName: got the name! -> "+usr.getName());
        return true;
      }
    }
    return false;
  }

  public boolean userLoggedIn(User user) {
    return this.userlist.elementAt(this.userlist.indexOf(user)).isLoggedIn();
  }
  
  public boolean loginUser(User user) {
    return this.userlist.elementAt(this.userlist.indexOf(user)).login();
  }
  
  public boolean logoutUser(User user) {
    return this.userlist.elementAt(this.userlist.indexOf(user)).logout();
  }
}
